<template>
  <div class="login">
    <img class="warp-bg" :src="state.bgUrl" style="width: 100%; height: 100%;" :object-fit="isMobile()? 'fill' : 'cover'">
    <el-form
      ref="loginForm"
      :style="{ width: isMobile() ? '280px' : '460px', marginRight: isMobile() ? '' : '200px' }"
      :model="state.loginForm"
      :rules="state.loginRules"
      class="login-form"
      :class="{ 'm-form': isMobile() }">
      <h3 class="title" style="cursor:pointer" @click="$router.push('/view')">
        <img :src="logoUrl" style="width: 24px;height: 24px;margin-right: 10px;"></img>
        {{ appTitle() }} - 登录页
      </h3>
      <el-form-item prop="username">
        <el-input v-model="state.loginForm.username" type="text" auto-complete="off" placeholder="账号" @blur="handleUsernameChange" @change="handleUsernameChange">
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
          <template #suffix>
            <div style="width: 14px;"></div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="state.loginForm.password" :type="state.showPwd ? 'text' : 'password'" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
          <template #suffix>
            <el-icon @click="showPwd">
              <Hide v-show="state.showPwd" />
              <View v-show="!state.showPwd" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item class="code-input" prop="code" v-if="state.captchaEnabled">
        <el-input v-model="state.loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
          <template #prefix>
            <el-icon><More /></el-icon>
          </template>
        </el-input>
        <div @click="getCode" class="code-img" v-html="state.codeUrl"></div>
      </el-form-item>

      <el-checkbox v-model="state.loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
      <el-form-item style="width: 100%">
        <el-button :loading="state.loading" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
          <span v-if="!state.loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="option">
          <div v-if="state.register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>

          <div v-if="state.resetpwd">
            <router-link class="link-type" :to="'/resetpwd'">忘记密码</router-link>
          </div>
        </div>
        <div class="login-tip">访客账号：visitor <span style="margin: 0 10px;"></span> 密码：visitor123</div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <div class="pc-footer" v-if="!isMobile()">
        <span>{{ appPCFooter() }}</span>
        <span style="margin-left: 20px; cursor: pointer" class="pointer" @click="toBeianAddress">备案号: {{ getBeianText() }}</span>
      </div>
      <div class="m-footer" v-else>
        <div>{{ appPCFooter() }}</div>
        <div style="cursor: pointer" class="pointer" @click="toBeianAddress">备案号: {{ getBeianText() }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="Login">
import { getCodeImg } from '@/api/login';
import logoUrl from '@/assets/images/logo.png';
import { useAppStore } from '@/store/common';
import { usePGCStore } from '@/store/projectGloabalConfig';
import { appPCFooter, appTitle, isMobile, getBeianText,toBeianAddress, changeBgImg } from '@/utils/common';
import { decrypt, encrypt } from '@/utils/jsencrypt';
import { ElMessage, ElNotification } from 'element-plus';
import Cookies from 'js-cookie';
import { reactive, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();
const store = useAppStore();
const pgc = usePGCStore();

let themeColor = ref(pgc.themeColor || '#000');

let align = ref(isMobile() ? 'center' : 'flex-end');
const state = reactive({
  codeUrl: '',
  loginForm: {
    username: '',
    password: '',
    rememberMe: false,
    code: '',
    uuid: ''
  },
  loginRules: {
    username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
    password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
    code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
  },
  showPwd: false,
  //
  loading: false,
  // 验证码开关
  captchaEnabled: true,
  // 注册开关
  register: true,
  // 重置密码
  resetpwd: true,
  redirect: undefined,
  formWidth: 460,
  bgUrl:changeBgImg()
});

const loginForm = ref();

const showPwd = () => {
  state.showPwd = !state.showPwd;
};

const getCookie = () => {
  const username = Cookies.get('username');
  const password = Cookies.get('password');
  const rememberMe = Cookies.get('rememberMe');
  state.loginForm = {
    username: username === undefined ? state.loginForm.username : username,
    password: password === undefined ? state.loginForm.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
};


const getCode = () => {
  getCodeImg().then(res => {
    state.codeUrl = (res as any).img;
    state.loginForm.code = (res as any).text
  });
};
getCode();

// 监听用户名 自动补全密码
const handleUsernameChange = () => {
  if(state.loginForm.username === 'visitor') {
    state.loginForm.password = 'visitor123'
  }
}

const handleLogin = () => {
  loginForm.value.validate(valid => {
    if (valid) {
      state.loading = true;
      if (state.loginForm.rememberMe) {
        Cookies.set('username', state.loginForm.username, { expires: 30 });
        Cookies.set('password', encrypt(state.loginForm.password), {
          expires: 30
        });
        Cookies.set('rememberMe', state.loginForm.rememberMe, {
          expires: 30
        });
      } else {
        Cookies.remove('username');
        Cookies.remove('password');
        Cookies.remove('rememberMe');
      }
      store
        .LOGIN({ ...state.loginForm, password: encrypt(state.loginForm.password) })
        .then(res => {
          setTimeout(() => {
            if(state.loginForm.username === 'visitor') {
              ElNotification({
                title: '提示',
                message: '您登录的是访客角色！隐私信息已加密处理,操作已限制！如需更多权限请联系管理员！',
                type: 'warning',
                duration: 5000,
              })
            }
             ElMessage.success('登录成功！');
             state.loading = false;
            router.push({ path: state.redirect || '/' });
           }, 1000)
        })
        .catch(err => {
          state.loading = false;
          getCode();
        });
    }
  });
};

getCookie();

watch(
  () => route,
  () => {
    state.redirect = route.query && route.query.redirect;
  },
  { immediate: true }
);

watch(
  () => pgc.themeColor,
  newVal => {
    themeColor.value = newVal;
  }
);

watch(() => state.loginForm.username,(n) => {
  handleUsernameChange()
},{ immediate: true })
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: v-bind(align);
  align-items: center;
  height: 100%;
  background: linear-gradient(
      90deg,
      rgba(247, 149, 51, 0.1),
      rgba(243, 112, 85, 0.1) 15%,
      rgba(239, 78, 123, 0.1) 30%,
      rgba(161, 102, 171, 0.1) 44%,
      rgba(80, 115, 184, 0.1) 58%,
      rgba(16, 152, 173, 0.1) 72%,
      rgba(7, 179, 155, 0.1) 86%,
      rgba(109, 186, 130, 0.1)
    ),
    white;
}
.warp-bg {
  position: absolute;
  pointer-events: none;

}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form {
  position: absolute;
  border-radius: 6px;
  background: #ffffff;
  background-image: url('../../assets/images/form_bg.png');
  background-size: 100% 100%;
  padding: 25px 25px 10px 25px;
  border: 1px solid #bfbfbf;
  box-shadow: 2px 1px 1px #bfbfbf;
  .el-input {
    height: 38px;
    input {
      padding-left: 12px;
      height: 32px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.m-form {
  min-width: 86%;
  max-width: 460px;
}
.login-tip {
  margin-top: 10px;
  font-size: 13px;
  text-align: center;
  color: v-bind(themeColor);
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  line-height: 30px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  text-shadow: 1px 1px 1px #000;
}

.option {
  margin-top: 10px;
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.code-input {
  display: flex;
  align-items: center;
  .el-input {
    flex: 1;
  }
  .code-img {
    height: 38px;
    width: 90px;
    margin-left: 10px;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    cursor: pointer;
  }
}
</style>
